local que_qiyun = fk.CreateSkill {
    name = "que_qiyun",
}
Fk:loadTranslationTable {
    ["que_qiyun"] = "奇运",
    [":que_qiyun"] = "出牌阶段限一次，你可以进行判定，若点数小于X，你获得一张点数A的牌，反之则获得点数9的牌，若均不符合，各获得一张(X为上次判定的点数，优先从场上获得)。",

    ["@que_qiyun"] = "奇运",
}

que_qiyun:addEffect("active", {
    anim_type = "drawcard",
    can_use = function(self, player)
        return player:usedSkillTimes(que_qiyun.name, Player.HistoryPhase) == 0
    end,
    card_filter = Util.FalseFunc,
    on_use = function(self, room, effect)
        local player = effect.from
        local mark = player:getMark("que_qiyun")
        local judge = {
            who = player,
            reason = que_qiyun.name,
            pattern = ".",
        }
        room:judge(judge)
        room:setPlayerMark(player, "que_qiyun", judge.card.number)
        if mark == 0 or (mark > 0 and (judge.card.number > mark or judge.card.number == mark)) then
            local cards = {}
            for _, id in ipairs(room:getOtherPlayers(player)) do
                for _, card in ipairs(id:getCardIds("hej")) do
                    if Fk:getCardById(card).number == 9 then
                        table.insertIfNeed(cards, card)
                    end
                end
            end
            if #cards > 0 then
                room:obtainCard(player, table.random(cards, 1), false, fk.ReasonPrey, player, que_qiyun.name,
                    "@@que_jiu-inhand")
            else
                local card = room:getCardsFromPileByRule(".|9")
                if #card > 0 then
                    room:moveCardTo(card, Player.Hand, player, fk.ReasonPrey, que_qiyun.name, nil, false, player,
                        "@@que_jiu-inhand")
                end
            end
        end
        if mark == 0 or (mark > 0 and (judge.card.number < mark or judge.card.number == mark)) then
            local cards = {}
            for _, id in ipairs(room:getOtherPlayers(player)) do
                for _, card in ipairs(id:getCardIds("hej")) do
                    if Fk:getCardById(card).number == 1 then
                        table.insertIfNeed(cards, card)
                    end
                end
            end
            if #cards > 0 then
                room:obtainCard(player, table.random(cards, 1), false, fk.ReasonPrey, player, que_qiyun.name,
                    "@@que_yao-inhand")
            else
                local card = room:getCardsFromPileByRule(".|1")
                if #card > 0 then
                    room:moveCardTo(card, Player.Hand, player, fk.ReasonPrey, que_qiyun.name, nil, false, player,
                        "@@que_yao-inhand")
                end
            end
        end
    end,
})
return que_qiyun
